﻿<cfsilent>
	<cfscript>

		/* 处理当前排课学期数据 */
		event.setArg("pageTitle", "教学运行管理 - 考试安排 - 生成课程考试计划");
		
		termId = event.getArg("TermID");

		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");
		
		currentTab = event.getArg("TabID", "labCourse");
		
		/* 生成任课单位列表 */
		
		sql = "SELECT 
					count(*) task_Count, 
					sum(a.task_chooesd) task_chooesd, 
					count(distinct a.cid) cid, 
					b.institute_id, 
					c.institute_name 
				FROM t_task a  
					 INNER JOIN t_course b ON b.cid = a.cid 
					 INNER JOIN t_institute c ON c.institute_id = b.institute_id 
				WHERE a.term_id = :termId 
					  AND 
					  SUBSTR(a.task_prop, 7, 1) = '0'
				GROUP BY 
					  b.institute_id, 
					  c.institute_name ";
				
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		
		rs_task = queryObj.execute( sql=sql ).getResult();
		
		sql = "SELECT 
					count(*) task_Count, 
					sum(a.task_chooesd) task_chooesd, 
					count(distinct a.cid) cid, 
					b.institute_id, 
					c.institute_name 
				FROM t_common_task a  
					 INNER JOIN t_course b ON b.cid = a.cid 
					 INNER JOIN t_institute c ON c.institute_id = b.institute_id 
				WHERE a.term_id = :termId 
					  AND 
					  SUBSTR(a.task_prop, 7, 1) = '0'
				GROUP BY 
					  b.institute_id, 
					  c.institute_name ";
				
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		
		rs_commonTask = queryObj.execute( sql=sql ).getResult();
		
		sql = "SELECT 
					count(*) task_Count, 
					sum(a.task_chooesd) task_chooesd, 
					count(distinct a.cid) cid, 
					b.institute_id, 
					c.institute_name 
				FROM t_reopen_task a  
					 INNER JOIN t_course b ON b.cid = a.cid 
					 INNER JOIN t_institute c ON c.institute_id = b.institute_id 
				WHERE a.term_id = :termId 
					  AND 
					  SUBSTR(a.task_prop, 7, 1) = '0'
				GROUP BY 
					  b.institute_id, 
					  c.institute_name ";
				
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		
		rs_openTask = queryObj.execute( sql=sql ).getResult();
		
		sql = "SELECT 
					count(*) task_Count, 
					sum(a.task_chooesd) task_chooesd, 
					count(distinct a.cid) cid, 
					b.institute_id, 
					c.institute_name 
				FROM t_sport_task a  
					 INNER JOIN t_course b ON b.cid = a.cid 
					 INNER JOIN t_institute c ON c.institute_id = b.institute_id 
				WHERE a.term_id = :termId 
					  AND 
					  SUBSTR(a.task_prop, 7, 1) = '0'
				GROUP BY 
					  b.institute_id, 
					  c.institute_name ";
				
		queryObj = new query( datasource=application.dnsSlave );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		
		rs_sportTask = queryObj.execute( sql=sql ).getResult();
		
		insTaskList = "";
		for (a=1; a LTE rs_task.recordCount; a++) {
			insTaskList = listAppend(insTaskList, "['P','" & rs_task["institute_id"][a] & "']");
		}

		for (a=1; a LTE rs_commonTask.recordCount; a++) {
			insTaskList = listAppend(insTaskList, "['C','" & rs_commonTask["institute_id"][a] & "']");
		}

		for (a=1; a LTE rs_openTask.recordCount; a++) {
			insTaskList = listAppend(insTaskList, "['R','" & rs_openTask["institute_id"][a] & "']");
		}
		
		for ( a=1; a LTE rs_sportTask.recordCount; a++ ) {
			insTaskList = listAppend(insTaskList, "['T','" & rs_sportTask["institute_id"][a] & "']");
		}

	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('examPlan')#</cfoutput>"><i class="mrs img btnList"></i><span class="uiButtonText">考试课程设置 </span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 db"></i>初始化考试安排计划
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix" id="instituteList">
					
				<div class="UIProgressbar">
					<span class="progressBar" id="pb1">0%</span>
					<p id="output">处理队列初始化</p>
				</div>
				
				<cfset row = 0 />
				<table class="UIEditable">
					<thead>
						<tr>
							<td width="18"></td>
							<td>开课类别</td>
							<td>学院</td>
							<td>开课门数</td>
							<td>教学班</td>
							<td>选课人数</td>
							<td width="18"></td>
						</tr>
					</thead>
					<tbody>
                        <cfloop query="rs_task"  >
							<cfset row ++ />
							<tr class="editRows" rowid="P<cfoutput>#rs_task.institute_id#</cfoutput>">
								<td><span class="index"><cfoutput>#row#</cfoutput></span></td>
								<td>计划课程</td>
								<td><cfoutput>#rs_task.institute_name#</cfoutput></td>
								<td><span class="numeric"><cfoutput>#rs_task.cid#</cfoutput></span></td>
								<td><span class="numeric"><cfoutput>#rs_task.task_Count#</cfoutput></span></td>
								<td><span class="numeric"><cfoutput>#rs_task.task_chooesd#</cfoutput></span></td>
								<td><span class="stat todo"></span></td>
							</tr>
						</cfloop>
                        <cfloop query="rs_commonTask"  >
							<cfset row ++ />
							<tr class="editRows" rowid="C<cfoutput>#rs_commonTask.institute_id#</cfoutput>">
								<td><span class="index"><cfoutput>#row#</cfoutput></span></td>
								<td>全校通选课</td>
								<td><cfoutput>#rs_commonTask.institute_name#</cfoutput></td>
								<td><span class="numeric"><cfoutput>#rs_commonTask.cid#</cfoutput></span></td>
								<td><span class="numeric"><cfoutput>#rs_commonTask.task_Count#</cfoutput></span></td>
								<td><span class="numeric"><cfoutput>#rs_commonTask.task_chooesd#</cfoutput></span></td>
								<td><span class="stat todo"></span></td>
							</tr>
						</cfloop>
                        <cfloop query="rs_openTask"  >
							<cfset row ++ />
							<tr class="editRows" rowid="R<cfoutput>#rs_openTask.institute_id#</cfoutput>">
								<td><span class="index"><cfoutput>#row#</cfoutput></span></td>
								<td>单开班课程</td>
								<td><cfoutput>#rs_openTask.institute_name#</cfoutput></td>
								<td><span class="numeric"><cfoutput>#rs_openTask.cid#</cfoutput></span></td>
								<td><span class="numeric"><cfoutput>#rs_openTask.task_Count#</cfoutput></span></td>
								<td><span class="numeric"><cfoutput>#rs_openTask.task_chooesd#</cfoutput></span></td>
								<td><span class="stat todo"></span></td>
							</tr>
						</cfloop>
					</tbody>
				</table>
				
				<!---<div id="out"></div>--->
				
				<script language="javascript" type="text/javascript">
					//<![CDATA[
					<!--
					$(document).ready(function() {
												
						/* init Progress */
						$("#pb1").progressBar({
							boxImage		: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
							barImage		: {
												0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
												30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
												70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
												}
							});
						
						var institute = [<cfoutput>#insTaskList#</cfoutput>];
						
						/* built each exam task by institute */
						function progress(current) {
														
							$.post(
									"<cfoutput>#buildURL('apiRebuildInstituteExamPlan')#</cfoutput>",
									{
										'DepID': institute[current][1],
										'TT': institute[current][0],
										'TID': '<cfoutput>#termId#</cfoutput>',
										'seed': (new Date()).getTime()
									},
									function(data){
										
										//$("#out").html(data);
										
										response = $.parseJSON(data);
										
										/* fresh grid stats icons */
										var rowid = response.taskType + response.institute.InstituteID;
										var $row = $("tr.editRows[rowid='" + rowid + "']", ".UIEditable");										
										$("span.stat", $row).removeClass("todo").addClass("checked");
										
										/* fresh progress bar */
										var percentage = Math.floor(100 * parseInt(current + 1) / parseInt(institute.length));
										
										$("#pb1").progressBar(
																percentage,
																{
																	boxImage : '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
																	barImage : {
																		0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
																		30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
																		70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
																},
																callback: function(running){
																	
																	if (running.running_value == running.value) {
																	
																		current++;
											
																		if ( current < institute.length) {
																			progress(current);
																		}
																		else {
																			$("#output").text("完成");
																		}
																	}
																}
																});
										
										/* fresh message */
										$("#output").text("正在处理学院开设课程 " + response.institute.InstituteName);
										
										
									});
							
						}
						
						progress(0);
							
					});
					// -->
					//]]>				
				</script>
				
			</div>
		</li>
	</ul>
</div>